<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Validate;

class Competitor extends Common
{
	public function firm_huishou($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="cf.is_deleted=0 and cf.is_actived=0 ";
		
    	$sum=Db::table("competitor_firm")->alias("cf")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor_firm")->alias("cf")
        ->join("users uc","cf.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","cf.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("cf.id")
    	->limit($start,$list)
        ->field("cf.id,cf.firm_no,cf.firm_name,cf.remark,ec.em_name as cname,cf.created_on,em.em_name as mname,cf.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("cfarr",$cfarr);
		return $this->fetch('firm_huishou');
	}
	//竞品厂商管理
	public function firm_manage($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="cf.is_deleted=0 and cf.is_actived=1 ";
		if($data){
    		if(trim($data["firm_no"]))
    			$where.="and cf.firm_no like '%{$data["firm_no"]}%' ";
    		if(trim($data["firm_name"]))
    			$where.="and cf.firm_name like '%{$data["firm_name"]}%' ";
    	}else{
    		$data=["firm_no"=>"","firm_name"=>""];
    	}

    	
    	$sum=Db::table("competitor_firm")->alias("cf")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor_firm")->alias("cf")
        ->join("users uc","cf.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","cf.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("cf.id")
    	->limit($start,$list)
        ->field("cf.id,cf.firm_no,cf.firm_name,cf.remark,ec.em_name as cname,cf.created_on,em.em_name as mname,cf.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("cfarr",$cfarr);
		return $this->fetch('firm_manage');
	}
	//竞品添加修改页面
	public function addupcf($cfid=""){
		


    	if($cfid){
    		$data=Db::table("competitor_firm")->where(["id"=>$cfid])
    		->field("firm_no,firm_name,remark")->find();

    	}else{
    		$data=["firm_no"=>"","firm_name"=>"","remark"=>""];
    	}
		$this->assign("data",$data);
		$this->assign("cfid",$cfid);
		return $this->fetch('addupcf');
	}
	//添加修改操作
	public function add_firm(){
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        $cfid=trim($datas["cfid"]);
      
        $datam["firm_name"]=htmlspecialchars(trim($datas["firm_name"]));
        $datam["firm_no"]=htmlspecialchars(trim($datas["firm_no"]));
       
        $datam["remark"]=htmlspecialchars(trim($datas["remark"]));
         if(!$datam["firm_name"])
        	return json_encode(["msg"=>"竞品厂商名称不能为空","state"=>2]);
        if($cfid){
        	//修改
        	////修改
        	////判断重复
        	$ischongfu=Db::table("competitor_firm")->where("is_deleted=0 and id<>{$cfid} and (firm_no='{$datam["firm_no"]}' or firm_name='{$datam["firm_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品厂商名或编号已存在","state"=>2]);
        	$datam["m_users_id"]=Session::get("login_id");
			$datam["modifiled_on"]=date("Y-m-d H:i:s");
			$datam["id"]=$cfid;
			Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor_firm")->update($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"操作已成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }else{
        	//新增
        	//增加
        	if(!$datam["firm_no"]){
        		//没有编号自动生成
        		
        		$datam["firm_no"]="CF".rand(0,100).time().Session::get("login_id");
        	}
        	//判断重复
        	$ischongfu=Db::table("competitor_firm")->where("is_deleted=0 and (firm_no='{$datam["firm_no"]}' or firm_name='{$datam["firm_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品厂商名或编号已存在","state"=>2]);
        	$datam["c_users_id"]=Session::get("login_id");
        	$datam["created_on"]=date("Y-m-d H:i:s");
        	Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor_firm")->insert($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"竞品厂商名称:{$datam["firm_name"]}(编号:{$datam["firm_no"]})添加成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }
	}
	//删除
	public function dropcf(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor_firm")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复
	public function huifucf(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor_firm")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}




	////////竞品品类
	///
	public function category_huishou($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="cf.is_deleted=0 and cf.is_actived=0 ";
		
    	$sum=Db::table("competitor_category")->alias("cf")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor_category")->alias("cf")
        ->join("users uc","cf.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","cf.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("cf.id")
    	->limit($start,$list)
        ->field("cf.id,cf.category_no,cf.category_name,cf.remark,ec.em_name as cname,cf.created_on,em.em_name as mname,cf.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("cfarr",$cfarr);
		return $this->fetch('category_huishou');
	}
	//竞品品类管理
	public function category_manage($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="cf.is_deleted=0 and cf.is_actived=1 ";
		if($data){
    		if(trim($data["category_no"]))
    			$where.="and cf.category_no like '%{$data["category_no"]}%' ";
    		if(trim($data["category_name"]))
    			$where.="and cf.category_name like '%{$data["category_name"]}%' ";
    	}else{
    		$data=["category_no"=>"","category_name"=>""];
    	}

    	
    	$sum=Db::table("competitor_category")->alias("cf")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor_category")->alias("cf")
        ->join("users uc","cf.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","cf.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("cf.id")
    	->limit($start,$list)
        ->field("cf.id,cf.category_no,cf.category_name,cf.remark,ec.em_name as cname,cf.created_on,em.em_name as mname,cf.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("cfarr",$cfarr);
		return $this->fetch('category_manage');
	}

	//删除
	public function dropcc(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor_category")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复
	public function huifucc(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor_category")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}

		//竞品添加修改页面
	public function addupcc($cfid=""){
		


    	if($cfid){
    		$data=Db::table("competitor_category")->where(["id"=>$cfid])
    		->field("category_no,category_name,remark")->find();

    	}else{
    		$data=["category_no"=>"","category_name"=>"","remark"=>""];
    	}
		$this->assign("data",$data);
		$this->assign("cfid",$cfid);
		return $this->fetch('addupcc');
	}
	//添加修改操作
	public function add_category(){
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        $cfid=trim($datas["cfid"]);
      
        $datam["category_name"]=htmlspecialchars(trim($datas["category_name"]));
        $datam["category_no"]=htmlspecialchars(trim($datas["category_no"]));
       
        $datam["remark"]=htmlspecialchars(trim($datas["remark"]));
         if(!$datam["category_name"])
        	return json_encode(["msg"=>"竞品品类名称不能为空","state"=>2]);
        if($cfid){
        	//修改
        	////修改
        	////判断重复
        	$ischongfu=Db::table("competitor_category")->where("is_deleted=0 and id<>{$cfid} and (category_no='{$datam["category_no"]}' or category_name='{$datam["category_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品品类名或编号已存在","state"=>2]);
        	$datam["m_users_id"]=Session::get("login_id");
			$datam["modifiled_on"]=date("Y-m-d H:i:s");
			$datam["id"]=$cfid;
			Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor_category")->update($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"操作已成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }else{
        	//新增
        	//增加
        	if(!$datam["category_no"]){
        		//没有编号自动生成
        		
        		$datam["category_no"]="CC".rand(0,100).time().Session::get("login_id");
        	}
        	//判断重复
        	$ischongfu=Db::table("competitor_category")->where("is_deleted=0 and (category_no='{$datam["category_no"]}' or category_name='{$datam["category_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品品类名或编号已存在","state"=>2]);
        	$datam["c_users_id"]=Session::get("login_id");
        	$datam["created_on"]=date("Y-m-d H:i:s");
        	Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor_category")->insert($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"竞品品类名称:{$datam["category_name"]}(编号:{$datam["category_no"]})添加成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }
	}

	////竞品管理
	public function manage($page=1,$list=10){

		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="c.is_deleted=0 and c.is_actived=1 ";
		if($data){
    		if(trim($data["c_name"]))
    			$where.="and c.c_name like '%{$data["c_name"]}%' ";
    		if(trim($data["category_name"]))
    			$where.="and cc.category_name like '%{$data["category_name"]}%' ";
    		if(trim($data["firm_name"]))
    			$where.="and cf.firm_name like '%{$data["firm_name"]}%' ";
    		if(trim($data["format"]))
    			$where.="and c.format='{$data["format"]}' ";
    	}else{
    		$data=["c_name"=>"","category_name"=>"","firm_name"=>"","format"=>""];
    	}

    	
    	$sum=Db::table("competitor")->alias("c")
    	->join("competitor_firm cf","c.firm_id=cf.id","LEFT")
    	->join("competitor_category cc","c.category_id=cc.id","LEFT")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor")->alias("c")
    	->join("competitor_firm cf","c.firm_id=cf.id","LEFT")
    	->join("competitor_category cc","c.category_id=cc.id","LEFT")
        ->join("users uc","c.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","c.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("c.id")
    	->limit($start,$list)
        ->field("c.id,c.c_no,c.c_name,cf.firm_name,cf.is_actived as cfstate,cc.category_name,cc.is_actived as ccstate,c.format,ec.em_name as cname,c.created_on,em.em_name as mname,c.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		$this->assign("data",$data);
		$this->assign("cfarr",$cfarr);
		return $this->fetch('manage');
	}
		//竞品添加修改页面
	public function addupc($cfid=""){
		


    	if($cfid){
    		$data=Db::table("competitor")->alias("c")
    	->where(["c.id"=>$cfid])
    	->field("c.c_no,c.category_id,c.firm_id,c.c_name,c.format,c.low_price,c.high_price,c.origin,c.description,c.remark")->find();

    	}else{
    		$data=["c_no"=>"","category_id"=>"","c_name"=>"","firm_id"=>"","format"=>"","low_price"=>"","high_price"=>"","origin"=>"","description"=>"","remark"=>""];
    	}
    	$cfarr=Db::table("competitor_firm")->where(["is_actived"=>1,"is_deleted"=>0])->select();
		$ccarr=Db::table("competitor_category")->where(["is_actived"=>1,"is_deleted"=>0])->select();

    	$this->assign("cfarr",$cfarr);
    	$this->assign("ccarr",$ccarr);
		$this->assign("data",$data);
		$this->assign("cfid",$cfid);
		return $this->fetch('addupc');
	}

	//添加修改操作
	public function add_com(){
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        $cfid=trim($datas["cfid"]);
      
        $datam["c_name"]=htmlspecialchars(trim($datas["c_name"]));
        $datam["c_no"]=htmlspecialchars(trim($datas["c_no"]));
       	$datam["origin"]=htmlspecialchars(trim($datas["origin"]));
        $datam["format"]=htmlspecialchars(trim($datas["format"]));
        $datam["remark"]=htmlspecialchars(trim($datas["remark"]));
        $datam["description"]=htmlspecialchars(trim($datas["description"]));
        $datam["category_id"]=trim($datas["category_id"]);
        if(trim($datas["firm_id"]))
        	$datam["firm_id"]=trim($datas["firm_id"]);
        
        $datam["low_price"]=trim($datas["low_price"]);
        
        $datam["high_price"]=trim($datas["high_price"]);

         if(!$datam["c_name"])
        	return json_encode(["msg"=>"竞品名称不能为空","state"=>2]);
        if(!$datam["low_price"])
        	return json_encode(["msg"=>"价格区间不能为空","state"=>2]);
         if(!$datam["high_price"])
        	return json_encode(["msg"=>"价格区间不能为空","state"=>2]);
        if(!$datam["format"])
        	return json_encode(["msg"=>"规格不能为空","state"=>2]);
         if(!$datam["category_id"])
        	return json_encode(["msg"=>"竞品品类不能为空","state"=>2]);
        if( $datam["low_price"]>$datam["high_price"])
        	return json_encode(["msg"=>"最大价格不能低于最小价格","state"=>2]);
        if($cfid){
        	//修改
        	////修改
        	////判断重复
        	$ischongfu=Db::table("competitor")->where("is_deleted=0 and id<>{$cfid} and (c_no='{$datam["c_no"]}' or c_name='{$datam["c_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品名或编号已存在","state"=>2]);
        	$datam["m_users_id"]=Session::get("login_id");
			$datam["modifiled_on"]=date("Y-m-d H:i:s");
			$datam["id"]=$cfid;
			Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor")->update($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"操作已成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }else{
        	//新增
        	//增加
        	if(!$datam["c_no"]){
        		//没有编号自动生成
        		
        		$datam["c_no"]="COM".rand(0,100).time().Session::get("login_id");
        	}
        	//判断重复
        	$ischongfu=Db::table("competitor")->where("is_deleted=0 and (c_no='{$datam["c_no"]}' or c_name='{$datam["c_name"]}')")->count();
        	if($ischongfu)
        		return json_encode(["msg"=>"竞品名或编号已存在","state"=>2]);
        	$datam["c_users_id"]=Session::get("login_id");
        	$datam["created_on"]=date("Y-m-d H:i:s");
        	Db::startTrans(); //开启事物
        	try{

        		$res1=Db::table("competitor")->insert($datam);

        	}catch(\Exception $e){
                  Db::rollBack();
                 return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }
             if($res1){
             	Db::commit();
             	return json_encode(["msg"=>"竞品名称:{$datam["c_name"]}(编号:{$datam["c_no"]})添加成功","state"=>1]);
             }else{
             	Db::rollBack();
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
             }

        }
	}
	//删除
	public function dropc(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复
	public function huifuc(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["cfid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("competitor")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	public function huishou($page=1,$list=10){
		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="c.is_deleted=0 and c.is_actived=0 ";
	

    	
    	$sum=Db::table("competitor")->alias("c")
    	->join("competitor_firm cf","c.firm_id=cf.id","LEFT")
    	->join("competitor_category cc","c.category_id=cc.id","LEFT")
    	->where($where)->count();


    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

        $cfarr=Db::table("competitor")->alias("c")
    	->join("competitor_firm cf","c.firm_id=cf.id","LEFT")
    	->join("competitor_category cc","c.category_id=cc.id","LEFT")
        ->join("users uc","c.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","c.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
    	->where($where)->order("c.id")
    	->limit($start,$list)
        ->field("c.id,c.c_no,c.c_name,cf.firm_name,cf.is_actived as cfstate,cc.category_name,cc.is_actived as ccstate,c.format,ec.em_name as cname,c.created_on,em.em_name as mname,c.modifiled_on")
        ->select();

          for($i=0;$i<count($cfarr);$i++){
        	$cfarr[$i]["created_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["created_on"]));
        	if($cfarr[$i]["modifiled_on"])
        		$cfarr[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($cfarr[$i]["modifiled_on"]));
        	
        }
         $this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		
		$this->assign("cfarr",$cfarr);
		return $this->fetch('huishou');
	}
}